package com.scrawler.util;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.impl.client.DefaultHttpClient;
import org.springframework.stereotype.Service;


@Service
public class HttpUtils {
	
	private static final Log LOG = LogFactory.getLog(HttpUtils.class);
	
	public  String getContentByUrl(String url){
		StringBuilder sb = new StringBuilder();
		if(url == null || !url.matches(ConstantUtils.URL_REGEXP_RULES)){
			return sb.toString();
		}
		 String line = null;  
		 BufferedReader  inReader = null;
		 HttpClient client = getHttpClient();
		 HttpGet request = new HttpGet(); 
		 request.setHeader("Connection", "close");
		try {  
            request.setURI(new URI(url));  
            HttpResponse response = client.execute(request);  
            inReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),ConstantUtils.CHARSET_GBK));                          
            while ((line = inReader.readLine())  != null) {  
                sb.append(line);  
            }                     
        } catch (URISyntaxException e) {
			// TODO Auto-generated catch block
        	LOG.error("HttpUtils getContentByUrl URISyntaxException: url="+url,e);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			LOG.error("HttpUtils getContentByUrl ClientProtocolException",e);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			LOG.error("HttpUtils getContentByUrl IOException ",e);
		} finally {  
            if (inReader != null) {  
                try {  
                	inReader.close();// 关闭reader  
                } catch (Exception e) {  
                    e.printStackTrace();  
                }  
            }            
        }
		return sb.toString();
	}
	
	public String getContentByUrl(String url,String enconding){
		StringBuilder sb = new StringBuilder();
		HttpClient client = getHttpClient();  
        InputStream in = null;  
        BufferedReader inReader = null;  
        HttpGet get = new HttpGet();  
        get.setHeader("Connection", "close");
        try  
        {  
            get.setURI(new URI(url));  
            HttpResponse response = client.execute(get);  
            if (response.getStatusLine ().getStatusCode () != 200) {  
                // close_wait 异常,被动关闭时,释放连接
            	get.abort();  
                return sb.toString();  
            }  
            HttpEntity entity =response.getEntity();         
            if( entity != null ){  
                in = entity.getContent();  
                String line = null;
                inReader = new BufferedReader(new InputStreamReader(in,enconding));                          
                while ((line = inReader.readLine())  != null) {  
                    sb.append(line);  
                    // sb.append(line + ConstantUtils.NEW_LINE);  
                }           
            }        
        }  catch (Exception e)  {  
        	// close_wait 异常,被动关闭时,释放连接
            get.abort();  
            e.printStackTrace ();  
            LOG.error(" get.abort().....",e);
            return sb.toString();  
        }finally{  
            if (inReader != null){  
                try  
                {  
                	inReader.close ();  
                }  
                catch (IOException e)  
                {  
                    e.printStackTrace ();  
                }  
            }  
            if (in != null){  
                try  
                {  
                    in.close ();  
                }  
                catch (IOException e)  
                {  
                    e.printStackTrace ();  
                }  
            }  
        }  
		return sb.toString();
	}
	
	public boolean executeUrl(String url){
		boolean flag = false;
		HttpClient client = getHttpClient();  
        HttpGet get = new HttpGet();  
       // get.setHeader("http.protocol.cookie-policy",CookiePolicy.IGNORE_COOKIES);
        get.setHeader("Connection", "close");
        try  
        {  
            get.setURI(new URI(url));  
            HttpResponse response = client.execute(get);  
            if (response.getStatusLine ().getStatusCode () != 200) {  
                // close_wait 异常,被动关闭时,释放连接
            	get.abort();  
                return flag;
            } 
            flag = true;
        }  catch (Exception e)  {  
        	// close_wait 异常,被动关闭时,释放连接
            get.abort();  
            e.printStackTrace ();  
            LOG.error(" get.abort().....",e);
        } finally{ 
        		get.abort();
        }  
         return flag;
	}
	
	public  HttpClient getHttpClient(){
		  HttpClient client = new DefaultHttpClient();  
		
		  //关闭设置代理
		  // HttpHost proxy = new HttpHost(ConstantUtils.PROXY_HOST, ConstantUtils.PROXY_PORT);   
          // client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);  
          // 解决 Cookie rejected:
          client.getParams().setParameter("http.protocol.cookie-policy",CookiePolicy.IGNORE_COOKIES);
          return client;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		
	}

}
